//
//  OneOfDTViewController.m
//  project_delicious
//
//  Created by 章历杰 on 15/3/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "OneOfDTViewController.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "FourProsCell.h"
#import "MBProgressHUD.h"
@interface OneOfDTViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableDictionary *data;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UIScrollView *SV;
@property (nonatomic, retain) MBProgressHUD *HUD;
@end

@implementation OneOfDTViewController
- (void)dealloc
{
    [_idNum release];
    [_mDic release];
    [_data release];
    [_array release];
    [_SV release];
    [_HUD release];
    [super dealloc];
}
//解析数据
- (void)startAnalyze:(NSString *)str
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.data = [NSMutableDictionary dictionary];
        self.data = [responseObject objectForKey:@"data"];
        
        self.array = [NSMutableArray array];
        self.array = [self.data objectForKey:@"dish"];
        
        //整个页面ScrollView
        self.SV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
        //顶层的专题图片&介绍
        UIImageView *topPic = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, self.SV.frame.size.width-20, 200)];
        NSString *str = [[self.data objectForKey:@"topic"] objectForKey:@"url"];
        str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str];
        [topPic setImageWithURL:url];
        topPic.layer.cornerRadius = 10;
        topPic.layer.masksToBounds = YES;
        [self.SV addSubview:topPic];
        //介绍
        UITextView *intro = [[UITextView alloc] initWithFrame:CGRectMake(10, 240, self.SV.frame.size.width-20, 200)];
        intro.text = [[self.data objectForKey:@"topic"] objectForKey:@"foreword"];
        intro.font = [UIFont systemFontOfSize:14];
        [intro sizeToFit];
        intro.editable = NO;
        intro.scrollEnabled = NO;
        intro.backgroundColor = [UIColor clearColor];
        [self.SV addSubview:intro];
        [intro release];
        
        //以下就tableView
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, intro.frame.origin.y+intro.frame.size.height+10, self.SV.frame.size.width, self.array.count*355) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        [self.SV addSubview:tableView];
        [tableView release];
        
        
        self.SV.contentSize = CGSizeMake(self.view.frame.size.width, tableView.frame.origin.y+tableView.frame.size.height);
        [self.view addSubview:self.SV];
        [_SV release];
        
        //返回按钮
        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 40, 40)];
        back.image = [UIImage imageNamed:@"backToRecomm.png"];
        back.layer.masksToBounds = YES;
        back.layer.cornerRadius = 20;
        back.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackAction)];
        [back addGestureRecognizer:tap];
        [tap release];
        [self.view addSubview:back];
        [back release];
        
        [_HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误");
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"shopDetailBackG.jpg"]];
    
    
    NSString *string1= @"http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_topic_topic_detail&parameters=%7B%22_topic_id%22%3A%22";
    NSString *string2 = @"%22%7D";
    
    NSString *str = [NSString stringWithFormat:@"%@%@%@",string1,self.idNum,string2];
    
    [self startAnalyze:str];
    
    self.HUD = self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    _HUD.labelText = @"Loading";
    //_HUD.color = [UIColor clearColor];
    _HUD.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_HUD];
    [_HUD release];
    [_HUD show:YES];
    
}
- (void)tapBackAction
{
    [self.navigationController popViewControllerAnimated:NO];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    FourProsCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[FourProsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    //target传navigation
    cell.target = self.navigationController;
    cell.mDic = [self.array objectAtIndex:indexPath.item];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 355;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
